package com.ramos.kuaixiao.network;

import android.os.AsyncTask;

import com.ramos.kuaixiao.App;
import com.ramos.kuaixiao.db.EmpStoreHouseDB;
import com.ramos.kuaixiao.db.WareHouseDB;
import com.ramos.kuaixiao.entity.EmpStoreHouse;
import com.ramos.kuaixiao.entity.WareHouse;
import com.ramos.kuaixiao.utils.CommUtil;
import com.ramos.kuaixiao.utils.FileUtil;
import com.ramos.kuaixiao.utils.SharedPreference;
import com.ramos.kuaixiao.utils.StringUtil;
import com.ramos.kuaixiao.utils.Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class DownloadEmpStorehouseTask extends AsyncTask<Void, Integer, Boolean> {
    private final static String TAG = "DownloadEmpStorehouse";
    String houseList = "";
    @Override
    protected void onPreExecute() {

    }

    @Override
    protected Boolean doInBackground(Void... params) {
        try {
            getAllLocalWareHouse();
            //getAllLocalEmpStorehouse();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    private void getAllLocalWareHouse() {
        List<WareHouse> tmpList = new ArrayList<>();
        File file = new File(FileUtil.getDataPath(App.mContext) + "/tbl_warehouse.csv");
        List<WareHouse> list = readWareHouseCsv(file.getAbsolutePath());
        CommUtil.d(App.mContext,TAG, "tbl_WareHouse:" + list.size());

        File file1 = new File(FileUtil.getDataPath(App.mContext) + "/tbl_emp_storehouse.csv");
        List<EmpStoreHouse> list1 = readEmpStoreHouseCsv(file1.getAbsolutePath());
        if(list1 != null && list1.size() > 0) {
            CommUtil.d(App.mContext,TAG, "tbl_empWareHouse:" + list.size());
            EmpStoreHouseDB empStoreHouseDB = new EmpStoreHouseDB();
            empStoreHouseDB.addAllCustomer(list1);

            for (EmpStoreHouse empStoreHouse : list1) {
                if (empStoreHouse.getEmpId().equals(SharedPreference.getPreferString("id"))) {
                    houseList = empStoreHouse.getStorehouseList();
                }
            }
        }

        if(!StringUtil.isEmpty(houseList)){
            String [] list2 =  houseList.split("_");
            if(list2.length > 0){
                for(String s : list2){
                    for(WareHouse wareHouse : list){
                        if(s.equals(wareHouse.getId())){
                            tmpList.add(wareHouse);
                        }
                    }
                }
            }
        } else {
            tmpList.addAll(list);
        }
        WareHouseDB shopsDB = new WareHouseDB();
        shopsDB.addAllWareHouse(tmpList);
    }


    private List<WareHouse> readWareHouseCsv(String absolutePath) {
        List<WareHouse> readerArr = new ArrayList<>();
        Scanner in = Utils.readCsv(absolutePath);
        try {
            in.nextLine();
            while (in.hasNextLine()) {
                String s = in.nextLine();
                String[] lines = s.split(",");
                /*if (Utils.replaceStr(lines[2]).equals(SharedPreference.getPreferString("dealer"))) {*/
                    WareHouse bean = new WareHouse(Utils.replaceStr(lines[0]), Utils.replaceStr(lines[1]), Utils.replaceStr(lines[4])
                            , "", Utils.replaceStr(lines[9]));
                    readerArr.add(bean);
                /*}*/
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return readerArr;
    }

    private void getAllLocalEmpStorehouse() {
        File file = new File(FileUtil.getDataPath(App.mContext) + "/tbl_emp_storehouse.csv");
        List<EmpStoreHouse> list = readEmpStoreHouseCsv(file.getAbsolutePath());
        EmpStoreHouseDB customerDB = new EmpStoreHouseDB();
        customerDB.addAllCustomer(list);
    }

    private List<EmpStoreHouse> readEmpStoreHouseCsv(String absolutePath) {
        List<EmpStoreHouse> readerArr = new ArrayList<>();
        Scanner in = Utils.readCsv(absolutePath);
        try {
            in.nextLine();
            while (in.hasNextLine()) {
                String s = in.nextLine();
                String[] lines = s.split(",");
                EmpStoreHouse bean = new EmpStoreHouse(Utils.replaceStr(lines[1]), Utils.replaceStr(lines[2]), Utils.replaceStr(lines[3]), Long.valueOf(1));
                readerArr.add(bean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return readerArr;
    }
}
